import {
    Text, View, ScrollView, StyleSheet, TouchableOpacity, Platform, Image, FlatList, SafeAreaView,
    Linking, ToastAndroid, TextInput, KeyboardAvoidingView, Alert, Dimensions
} from "react-native";

import AliyunOSS from 'aliyun-oss-react-native'
import HttpUtils from '../../utils/HttpUtils'
import HttpUrls from '../../utils/HttpUrls'
import MD5 from "react-native-md5"
import Constant from '../../utils/Constant'//全局变量
import moment from 'moment';

const isIos = Platform.OS == 'ios';

const endPoint = "https://oss-cn-hangzhou.aliyuncs.com"
const bucket = "imgrep"
const domain = "https://img.shuaiche.com/"

export default class Upload {

    static ossTypeImage = 0;
    static OSS_LICENSE = 1;
    static OSS_FACE = 2;
    static OSS_CAR = 3;
    static OSS_CONTRACT = 4;

    /**
     * 
     * @param {*} fpath 
     * @param {*} successFun 
     * @param {*} ossType 0：图片
     */
    static getAlipz = (fpath, ossType, successFun) => {
        // 获取凭证接口。
        HttpUtils.getRequest(HttpUrls.resourceAccess, {}).then((res) => {
            if (res.success === true) {
                let data = res.data;
                console.log('fpath====', '======================' + fpath)
                if (this.ossTypeImage == ossType || this.OSS_LICENSE == ossType || this.OSS_FACE == ossType || this.OSS_CAR == ossType || this.OSS_CONTRACT == ossType) {
                    this.uploadImage(data, fpath,ossType, successFun);
                }

            }
        })
    }

    static getPathPre = (ossType) => {
        let prefix = ''
        if (Constant.release) {
            if(this.OSS_CONTRACT == ossType){
                prefix = "file/contract/"
            }else{
                prefix = "img/"
            }
            
        } else {
            prefix = "temp/"
        }

         /**
     * OSS上传类型  1认证  2头像 3车辆图片 6 IM图片视频 7 IM文件 8 IM语音
     */if (ossType === this.OSS_LICENSE) {
            prefix = prefix + 'license/';
        }else if (ossType === this.OSS_FACE) {
            prefix = prefix + 'face/';
        }else{
            let timestamp = moment().format('YYYYMM');
            prefix = prefix + timestamp + '/';
        }
        
        return prefix;
    }

    /**
     * 基于 图片上传
     * @param url 请求URL
     * @param params 请求参数
     * @returns {Promise}
     */
    static uploadImage = (data, fpath,ossType, successFun) => {
        if (!isIos) {
            let temp = fpath.replace('file://', '')
            fpath = temp
        }
        AliyunOSS.initWithSecurityToken(data.securityToken, data.accessKeyId, data.accessKeySecret, endPoint);
        let prefix = this.getPathPre(ossType);
        let timestamp = Date.parse(new Date()).toString();
        let bojkey = prefix + MD5.hex_md5(timestamp + getRandom(10000, 100000)) + '.jpeg';
        console.log('fpath====', fpath)
        console.log('bojkey====', bojkey)
        setTimeout(() => {
            AliyunOSS.asyncUpload(bucket, bojkey, fpath).then((res) => {
                console.log('UploadRes=', res)
                if (res === 'UploadSuccess' || res.indexOf("x-oss-request-id") != -1) {
                    // 图片上传成功后， 解析图片。
                    let picUrl = domain + bojkey;
                    console.log('picurl=', picUrl)
                    successFun('/'+bojkey);
                }
            }).catch((error) => {
                console.log(error)
            })
        }, 500);
    }

}

function getRandom(start, end) {
    var length = end - start;
    var num = parseInt(Math.random() * (length) + start);
    return num;
}



